﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Cua_Hang.DTO;

namespace Cua_Hang.DAO
{
    public class Cua_Hang_DAO
    {
        //-----------load tat ca cua hang tren menu ngang ------------
        public static List<Cua_Hang_DTO> ListCuaHangAll_DAO()
        {
            try
            {
                    DataProvider d = new DataProvider();
                    String strSQL = "SELECT * FROM Cua_Hang";
                    DataTable dt = d.ExecuteQuery(strSQL);

                    List<Cua_Hang_DTO> lstCuaHang = new List<Cua_Hang_DTO>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        Cua_Hang_DTO dm = new Cua_Hang_DTO();
                        dm.ID_cuahang = (int)dr["ID_cuahang"];
                        dm.Ten_cuahang = dr["Ten_cuahang"].ToString();


                        lstCuaHang.Add(dm);
                    }
                    return lstCuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }

        //-------ket thuc load tat ca cua hang tren menu ngang------------
        //==================================================================

        //==================================================================
        //-------load tat ca cua hang theo id cua hang-----------
        public static List<Cua_Hang_DTO> ListCuaHangTheo_id_DAO(int ID_cuahang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang  WHERE ID_cuahang like '%" + ID_cuahang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<Cua_Hang_DTO> lst_CuaHang = new List<Cua_Hang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    Cua_Hang_DTO dm = new Cua_Hang_DTO();
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_cuahang = dr["Ten_cuahang"].ToString();

                    lst_CuaHang.Add(dm);
                }
                return lst_CuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //------- ket thuc load tat ca cua hang theo id cua hang-------------

        //==================================================================
        //-----------them cua hang tren menu ngang ------------
       
        public static void ThemCuaHang_DAO(Cua_Hang_DTO ch)
        {
            try
            {
                    DataProvider chang = new DataProvider();
                    String strSQL = "INSERT INTO Cua_Hang(Ten_cuahang) VALUES(";
                    //strSQL += "N'" + kh.Ten_KhachHang + "',";
                    //strSQL += "N'" + kh.DiaChi_KhachHang + "',";
                    //strSQL += "N'" + kh.Email_KhachHang + "',";
                    strSQL += "N'" + ch.Ten_cuahang + "')";
                    chang.ExecuteQuery(strSQL);
            }
            catch(Exception ex){
                throw new Exception("Loi!!" + ex.Message);
            }
        }

        
        //-------ket thuc them cua hang tren menu ngang ------------
        //==============================================================
        //------ xoa cua hang tren menu ngang ------------
        public static void XoaCuaHang_DAO(Cua_Hang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "DELETE FROM Cua_Hang WHERE ID_cuahang ='" + ch.ID_cuahang + "'";
                
                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc xoa cua hang tren menu ngang ------------

        //==============================================================
        //------ sua cua hang tren menu ngang ------------
        public static void SuaCuaHang_DAO(Cua_Hang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "UPDATE Cua_Hang SET Ten_cuahang = N'" + ch.Ten_cuahang + "' WHERE ID_cuahang = '" + ch.ID_cuahang + "'";
               // string updateCommand = "UPDATE HOCSINH " +
                                    // "SET Ten_cuahang = '" + ch.Ten_cuahang +
                                     
                                    //" DiaChi = '" + info.DiaChi + "', " +
                                    //" NgaySinh = '" + info.NgaySinh.ToShortDateString() + "', " +
                                    //" MaLop = '" + info.MaLop + "'," +
                                    //" GioiTinh = '" + info.GioiTinh + "', " +
                                    //" NamHoc = " + info.NamHoc +
                                 // " WHERE ID_cuahang = '" + ch.ID_cuahang + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc sua cua hang tren menu ngang ------------
        //==============================================================
        //------ ham kiem tra  cua hang co trong csdl hay chua ------------
        public static Boolean KiemTraTrung_CuaHang_DAO(Cua_Hang_DTO ch)
        {
            Boolean kq = false;
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang  WHERE Ten_cuahang =N'" + ch.Ten_cuahang + "'";
                DataTable dt = d.ExecuteQuery(strSQL);
                if(dt.Rows.Count > 0)
                {
                    kq = true;
                }
                
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
            return kq;
        }
        //-------ket thuc ham kiem tra  cua hang co trong csdl hay chua -----------

        //==============================================================
       
        

        

    }
}